package com.starfactory.common.shared;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 统一响应消息报文
 *
 * @param <T> T对象
 * @author caiqiang
 */
@Data
public class ResponseInfo<T>{

    /**
     * 状态码
     **/
    private String code;

    /**
     * 异常消息
     */
    private String message;


    /**
     * 业务数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    private ResponseInfo(String code, String message) {
        this(code, message, null);
    }

    private ResponseInfo(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    /**
     * 返回处理成功，业务数据为空
     *
     */
    public static <T> ResponseInfo<T> success() {
        return new ResponseInfo<>("10000", "处理成功");
    }


    /**
     * 返回处理成功，有业务数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseInfo<T> success(T data) {
        return new ResponseInfo<>("10000", "处理成功", data);
    }


    /**
     * 返回处理失败
     * @param code 状态码
     * @param message 异常消息
     * @return
     */
    public static <T> ResponseInfo<T> fail(String code, String message) {
        return new ResponseInfo<>(code, message);
    }

}
